package com.dycx.system.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.system.converter.NotifyConverter;
import com.dycx.system.dto.NotifyDTO;
import com.dycx.system.entity.NotifyDO;
import com.dycx.system.service.NotifyService;
import com.dycx.system.vo.NotifyVO;
import com.dycx.system.mapper.NotifyMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * (Notify)表服务实现类
 *
 * @author wuzongbin
 * @since 2023-12-04 22:58:56
 */

@Service
@AllArgsConstructor
public class NotifyServiceImpl extends ServiceImpl<NotifyMapper, NotifyDO> implements NotifyService {

    private NotifyMapper notifyMapper;

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<NotifyVO> getPage(NotifyDTO dto) {
        LambdaQueryWrapper<NotifyDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(dto.getId()), NotifyDO::getId, dto.getId());
        wrapper.orderByDesc(NotifyDO::getId);

        Page<NotifyDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        Page<NotifyVO> voPage = NotifyConverter.entityToVoPage(entityPage);
        voPage.getRecords().forEach(this::setData);

        return voPage;
    }

    /**
     * 查询详情
     *
     * @param id 主键
     * @return 查询结果
     */
    @Override
    public NotifyVO load(Integer id) {
        NotifyDO entity = notifyMapper.selectById(id);

        return setData(NotifyConverter.entityToVo(entity));
    }

    /**
     * 设置数据
     *
     * @param vo 实体
     * @return 查询结果
     */
    public NotifyVO setData(NotifyVO vo) {
        return vo;
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean create(NotifyDTO dto) {
        NotifyDO entity = BeanUtil.toBean(dto, NotifyDO.class);
        return save(entity);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean edit(NotifyDTO dto) {
        NotifyDO entity = BeanUtil.toBean(dto, NotifyDO.class);
        return updateById(entity);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public Boolean delete(Integer id) {
        return removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return notifyMapper.getMaxSort();
    }
}

